from math import sqrt

def pearson(rating1, rating2):
    """皮尔逊近似值计算函数实现"""
    sum_xy = 0
    sum_x = 0
    sum_y = 0
    sum_x2 = 0
    sum_y2 = 0
    n = 0
    for key in rating1:
        if key in rating2:
            n += 1
            x = rating1[key]
            y = rating2[key]
            sum_xy += x * y
            sum_x += x
            sum_y += y
            sum_x2 += x ** 2
            sum_y2 += y ** 2

    denominator = sqrt(sum_x2 - (sum_x**2) / n ) * sqrt(sum_y2 - (sum_y**2) / n)

    if denominator == 0:
        return 0
    else:
        return (sum_xy - (sum_x * sum_y) / n) / denominator


